﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Yipusoft.Evaluate.DBUtility;
using Yipusoft.Evaluate.Model;

namespace Yipusoft.Evaluate.DAL
{
    public class Check
    {
        public bool Exsits(int CheckID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@CheckID",SqlDbType.Int)
            };
            parameters[0].Value = CheckID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Checks_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public int Insert(CheckInfo Check)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@CheckID",SqlDbType.Int),
                new SqlParameter("@CheckName",SqlDbType.NVarChar,50),
                new SqlParameter("@CheckDate",SqlDbType.DateTime),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = Check.CheckName;
            parameters[2].Value = Check.CheckDate;
            parameters[3].Value = Check.TotalScore;
            parameters[4].Value = Check.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Checks_Insert", parameters);
                return (int)parameters[0].Value;
            }
        }
        public bool Update(CheckInfo Check)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@CheckID",SqlDbType.Int),
                new SqlParameter("@CheckName",SqlDbType.NVarChar,50),
                new SqlParameter("@CheckDate",SqlDbType.DateTime),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Value = Check.CheckID;
            parameters[1].Value = Check.CheckName;
            parameters[2].Value = Check.CheckDate;
            parameters[3].Value = Check.TotalScore;
            parameters[4].Value = Check.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Checks_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int CheckID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@CheckID",SqlDbType.Int)
            };
            parameters[0].Value = CheckID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Checks_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public CheckInfo GetCheck(int CheckID)
        {
            CheckInfo Check = null;
            SqlParameter[] parameters = {
                new SqlParameter("@CheckID",SqlDbType.Int)
            };
            parameters[0].Value = CheckID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Checks_GetDetail", parameters);
                if (dr.Read())
                {
                    Check = new CheckInfo();
                    Check.CheckID = dr.GetInt32(0);
                    Check.CheckName = dr.GetString(1);
                    Check.CheckDate = dr.GetDateTime(2);
                    Check.TotalScore = dr.GetDecimal(3);
                    Check.Description = dr.GetString(4);
                }
                return Check;
            }
        }
        public List<CheckInfo> GetList(string StrWhere)
        {
            List<CheckInfo> Checks = new List<CheckInfo>();
            CheckInfo Check = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Checks_GetList", parameters);
                while (dr.Read())
                {
                    Check = new CheckInfo();
                    Check.CheckID = dr.GetInt32(0);
                    Check.CheckName = dr.GetString(1);
                    Check.CheckDate = dr.GetDateTime(2);
                    Check.TotalScore = dr.GetDecimal(3);
                    Check.Description = dr.GetString(4);
                    Checks.Add(Check);
                }
                return Checks;
            }
        }
    }
}
